package com.smartscreen.contentmanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartscreen.contentmanage.entity.Notice;
import com.smartscreen.contentmanage.service.NoticeService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/content/notices")
@Validated
public class NoticeController {
    
    @Autowired
    private NoticeService noticeService;
    
    /**
     * 创建通知公告
     * @param notice 通知信息
     * @return 创建结果
     */
    @PostMapping
    public ApiResponse<Map<String, String>> createNotice(@RequestBody Notice notice) {
        try {
            if (notice == null) {
                return ApiResponse.badRequest("通知信息不能为空");
            }
            
            boolean success = noticeService.save(notice);
            
            if (success) {
                Map<String, String> data = new HashMap<>();
                data.put("noticeId", notice.getNoticeId());
                return ApiResponse.created("创建成功", data);
            } else {
                return ApiResponse.serverError("创建失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("创建失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取通知公告列表
     * @param page 页码
     * @param pageSize 每页大小
     * @param status 状态（可选）
     * @return 通知列表
     */
    @GetMapping
    public ApiResponse<Map<String, Object>> getNotices(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String status) {
        
        try {
            if (pageSize > 100) {
                return ApiResponse.badRequest("每页大小不能超过100");
            }
            
            IPage<Notice> pageResult = noticeService.getNoticeList(page, pageSize, status);
            
            Map<String, Object> data = new HashMap<>();
            data.put("total", pageResult.getTotal());
            data.put("notices", pageResult.getRecords());
            
            return ApiResponse.success("获取通知列表成功", data);
        } catch (Exception e) {
            return ApiResponse.serverError("获取通知列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新通知公告
     * @param noticeId 通知ID
     * @param notice 通知信息
     * @return 更新结果
     */
    @PutMapping("/{noticeId}")
    public ApiResponse<Void> updateNotice(
            @PathVariable String noticeId,
            @RequestBody Notice notice) {
        
        try {
            if (noticeId == null || noticeId.trim().isEmpty()) {
                return ApiResponse.badRequest("通知ID不能为空");
            }
            
            if (notice == null) {
                return ApiResponse.badRequest("通知信息不能为空");
            }
            
            notice.setNoticeId(noticeId);
            boolean success = noticeService.updateById(notice);
            
            if (success) {
                return ApiResponse.success("更新成功");
            } else {
                return ApiResponse.notFound("未找到指定通知");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("更新失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除通知公告
     * @param noticeId 通知ID
     * @return 删除结果
     */
    @DeleteMapping("/{noticeId}")
    public ApiResponse<Void> deleteNotice(@PathVariable String noticeId) {
        try {
            if (noticeId == null || noticeId.trim().isEmpty()) {
                return ApiResponse.badRequest("通知ID不能为空");
            }
            
            boolean success = noticeService.removeById(noticeId);
            
            if (success) {
                return ApiResponse.success("删除成功");
            } else {
                return ApiResponse.notFound("未找到指定通知");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("删除失败：" + e.getMessage());
        }
    }
}